package com.fangrui.controller;

import com.fangrui.pojo.Result;
import com.fangrui.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2025/4/25 15:40.
//
@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
//    @PostMapping("/upload")
//    public Result Upload(String username,String age,MultipartFile image) throws IOException {
//        log.info("内容：{},{},{}",username,age,image);
//        //将文件存储在指定目录
//        LocalDate localDate = LocalDate.now();
//        File filePath = new File("D:\\Items\\JavaItems\\JavaWeb\\tlias-web-management\\src\\main\\resources\\upload_file" + "\\" + localDate + "\\");
//        if (!filePath.exists()) {
//            if(!filePath.mkdirs() || image.isEmpty()) {
//                return Result.error("文件存储失败");
//            }
//        }
//        //MultipartFile的getName()是用来获取表单项的name值，getOriginalFilename()才是返回文件名
//        String fileType = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
//        String file = UUID.randomUUID().toString() + fileType;
//        image.transferTo(new File(filePath.getPath() + "\\" + file));
//        return Result.success();
//    }
    @PostMapping("/upload")
    public Result Upload(MultipartFile image) throws IOException {
        log.info("内容：{}",image);
        String imageUrl = aliOSSUtils.upload(image);
        return Result.success(imageUrl);
    }
}
